import Vue from 'vue'
import Vuex from 'vuex'
import { Object } from 'core-js'

// Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    articleTypeDic: {},
    tagDic: {},
    baseUrl: '',
    isNoArticle: false,
    scrollTop: 0
  },
  mutations: {
    AddToArticleTypeDic(state, value) {
      Vue.set(state.articleTypeDic, value.id, value)
    },
    AddToTagDic(state, value) {
      Vue.set(state.tagDic, value.id, value)
    },
    DeleteTag(state, value) {
      Vue.delete(state.tagDic, value)
    },
    DeleteArticleType(state, value) {
      Vue.delete(state.articleTypeDic, value)
    },
    ChangeHasArticle(state) {
      state.isNoArticle = !state.isNoArticle
    },
    Init(state) {
      Vue.prototype.$api.ArticleType.GetArticleTypeDic().then(
        res => (state.articleTypeDic = res.data)
      )
      Vue.prototype.$api.Tag.GetTagDic().then(res => (state.tagDic = res.data))
    },
    RecordScrollTop(state, n) {
      state.scrollTop = n
    }
  },
  actions: {
    Init({ commit }) {
      commit('Init')
    }
  },
  modules: {},
  getters: {
    hostPath: () => {
      let curWwwPath = window.document.location.href
      //获取主机地址之后的目录如：/Tmall/index.jsp
      let pathName = window.document.location.pathname
      let pos = curWwwPath.indexOf(pathName)
      //获取主机地址，如： http://localhost:8080
      let localhostPaht = curWwwPath.substring(0, pos)
      return localhostPaht
    },
    tagList: state => {
      let res = []
      for (const key in state.tagDic) {
        res.push(state.tagDic[key])
      }
      return res
    },
    articleTypeList: state => {
      let res = []
      Object.keys(state.articleTypeDic).forEach(key => {
        res.push(state.articleTypeDic[key])
      })
      return res
    }
  }
})
